package com.zjxny.jobs;

import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.zjxny.exception.TrafficException;
import com.zjxny.service.AccountService;
import com.zjxny.service.CarInfoService;
import com.zjxny.service.WxCpZjService;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.erupt.core.annotation.EruptHandlerNaming;
import xyz.erupt.job.handler.EruptJobHandler;
import xyz.erupt.jpa.dao.EruptDao;

@Slf4j
@Service
@EruptHandlerNaming("更新车辆档案")
public class CarListJob implements EruptJobHandler {

    private final Lock lock = new ReentrantLock();
    private boolean shouldSkip = false;

    @Resource
    private EruptDao eruptDao;

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    CarInfoService carInfoService;

    @Resource
    AccountService accountService;
    
    @Resource
	WxCpZjService wxCpZjService;

    @Override
    public String exec(String code, String param) {

        //任务执行过程中加锁
        lock.lock();
        try {
            if (shouldSkip) {
                System.err.println("Skipping task...");
                return null;
            }

            shouldSkip = true;

            // 执行任务的代码
            System.err.println("Executing task...");

            //查询车辆表中任务字段taskStatus为false的车辆
            List<Map<String, Object>> result = accountService.getTaskAccount();
            log.info("carListJob.invoked. result.size={}",result.size());
            if (null!=result && !result.isEmpty()) {
                for (Map<String, Object> map : result) {
                    Boolean hasOperation = Convert.toBool(map.get("has_operation"));
                    Boolean hasNotOperation = Convert.toBool(map.get("has_not_operation"));
                    Boolean hasBiz = Convert.toBool(map.get("has_biz"));
                    Long accountId = Convert.toLong(map.get("id"));
                    String simpleTitle = Convert.toStr(map.get("simple_title"));
                    
                    log.info("carListJob.invoked. company={},accountId={},hasOperation={}",simpleTitle,accountId,hasOperation);
                    try {
                        //租赁
                        if(hasBiz) {
                        	carInfoService.getBizCarList(accountId);
                        }
                        
                        //营运车辆
                        if (hasOperation) {
                            carInfoService.getOperationCarList(accountId);//运营车 TODO 测试阶段先关闭
                        }
                        
                        //非营运
                        if(hasNotOperation) {
                        	carInfoService.getCarList(accountId);//非营运车
                        }
                        
                        accountService.updateTaskStatus(accountId, 0);
                    } catch (TrafficException e) {
                    	log.warn("CarListJob.session.expire, toUser={}, account={}",e.getToUser(),e.getAccountName());
                    	
                        accountService.disableAccount(accountId);
                        accountService.updateTaskStatus(accountId, 1);
                        
                        if(StrUtil.isNotBlank(e.getToUser())) {				
            				wxCpZjService.sendNotifyMessage(e.getToUser(),String.format("%s会话过期,请及时处理!", e.getAccountName()));
            			}
                    }
                }
            }
            shouldSkip = false;
        } finally {
            shouldSkip = false;
            lock.unlock();
        }
        return null;
    }
}
